SQL Performance Explained Everything Developers Need to Know about SQL Performance by Winand Markus

SQL Performance Explained Everything Developers Need to Know about SQL Performance by Winand Markus

Author:Winand, Markus
Language: eng
Format: epub
Publisher: Markus Winand
Published: 2023-10-21T00:00:00+00:00


Warning

JPA and Hibernate return the employees for each sale.

That means that an employee with 30 sales will appear 30 times. Although it is very disturbing, it is the specified behavior (EJB 3.0 persistency, paragraph 4.4.5.3 “Fetch Joins”). You can either manually de-duplicate the parent relation or use the function distinct() as shown in the example.

CriteriaBuilder qb = em.getCriteriaBuilder();

CriteriaQuery<Employees> q = qb.createQuery(Employees.class); Root<Employees> r = q.from(Employees.class);

q.where(queryBuilder.like(

queryBuilder.upper(r.get(Employees_.lastName)), "WIN%")

);

r.fetch("sales", JoinType.LEFT);

// needed to avoid duplication of Employee records query.distinct(true);

List<Employees> emp = em.createQuery(query).getResultList();

Hibernate 3.6.0 generates the following SQL statement:

select distinct

employees0_.subsidiary_id as subsidiary1_0_0_

, employees0_.employee_id as employee2_0_0_

-- MORE COLUMNS

, sales1_.sale_id as sale1_0__

from employees employees0_

left outer join sales sales1_

on employees0_.subsidiary_id=sales1_.subsidiary_id and employees0_.employee_id=sales1_.employee_id where upper(employees0_.last_name) like ?

The query has the expected left join but also an unnecessary distinct keyword. Unfortunately, JPA does not provide separate API calls to filter duplicated parent entries without de-duplicating the child records as well. The distinctkeyword in the SQL query is alarming because most databases will actually filter duplicate records. Only a few databases recognize that the primary keys guarantees uniqueness in that case anyway.

The native Hibernate API solves the problem on the client side using a result set transformer:

Criteria c = session.createCriteria(Employees.class); c.add(Restrictions.ilike("lastName", 'Win%')); c.setFetchMode("sales", FetchMode.JOIN);

c.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); List<Employees> result = c.list();

It generates the following query:

select this_.subsidiary_id as subsidiary1_0_1_ , this_.employee_id as employee2_0_1_

-- MORE this_ columns on employees

, sales2_.sale_id as sale1_3_

-- MORE sales2_ columns on sales

from employees this_

left outer join sales sales2_

on this_.subsidiary_id=sales2_.subsidiary_id and this_.employee_id=sales2_.employee_id where lower(this_.last_name) like ?

This method produces straight SQL without unintended clauses. Note that Hibernate uses lower()for case-insensitive queries— an important detail for function-based indexing.

Perl

The following example uses Perl’s DBIx::Class framework:

my @employees =

$schema->resultset('Employees')

->search({ 'UPPER(last_name)' => {-like => 'WIN%'} , {prefetch => ['sales']}

});

DBIx::Class 0.08192 generates the following SQL statement:

SELECT me.employee_id, me.subsidiary_id, me.last_name

-- MORE COLUMNS

FROM employees me

LEFT JOIN sales sales

ON (sales.employee_id = me.employee_id AND sales.subsidiary_id = me.subsidiary_id) WHERE ( UPPER(last_name) LIKE ? )

ORDER BY sales.employee_id, sales.subsidiary_id

Note the order byclause — it was not requested by the application. The database has to sort the result set accordingly, and that might take a while.

PHP The following example uses PHP’s Doctrine framework:

$qb = $em->createQueryBuilder();

$qb->select('e,s')

->from('Employees', 'e')

->leftJoin('e.sales', 's')

->where("upper(e.last_name) like :last_name")

->setParameter('last_name', 'WIN%');

$r = $qb->getQuery()->getResult();

Doctrine 2.0.5 generates the following SQL statement:

SELECT e0_.employee_id AS employee_id0

-- MORE COLUMNS

FROM employees e0_

LEFT JOIN sales s1_

ON e0_.subsidiary_id = s1_.subsidiary_id AND e0_.employee_id = s1_.employee_id WHERE UPPER(e0_.last_name) LIKE ?

The execution plan shows the NESTED LOOPS OUTER operation:

--------------------------------------------------------------|Id |Operation | Name | Rows | Cost |

--------------------------------------------------------------| 0 |SELECT STATEMENT | | 822 | 38 | | 1 | NESTED LOOPS OUTER | | 822 | 38 | | 2 | TABLE ACCESS BY INDEX ROWID| EMPLOYEES | 1 | 4 | |*3 | INDEX RANGE SCAN | EMP_UP_NAME | 1 | | | 4 | TABLE ACCESS BY INDEX ROWID| SALES | 821 | 34 | |*5 | INDEX RANGE SCAN | SALES_EMP | 31 | |

--------------------------------------------------------------

Predicate Information (identified by operation id):

--------------------------------------------------

3 - access(UPPER("LAST_NAME") LIKE 'WIN%')

filter(UPPER("LAST_NAME") LIKE 'WIN%')

5 - access("E0_"."SUBSIDIARY_ID"="S1_"."SUBSIDIARY_ID"(+) AND "E0_"."EMPLOYEE_ID" ="S1_"."EMPLOYEE_ID"(+))

The database retrieves the result from the EMPLOYEEStable via EMP_UP_NAME first and fetches the corresponding records from the SALEStable for each employee afterwards.

Tip

Get to know your ORM and take control of joins.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Popular ebooks
Whisky: Malt Whiskies of Scotland (Collins Little Books) by dominic roskrow(55999)
What's Done in Darkness by Kayla Perrin(26585)
The Fifty Shades Trilogy & Grey by E L James(19072)
Shot Through the Heart: DI Grace Fisher 2 by Isabelle Grey(19053)
Shot Through the Heart by Mercy Celeste(18930)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 10 by Isuna Hasekura and Jyuu Ayakura(17103)
Python GUI Applications using PyQt5 : The hands-on guide to build apps with Python by Verdugo Leire(16971)
Peren F. Statistics for Business and Economics...Essential Formulas 3ed 2025 by Unknown(16861)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 03 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16813)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 01 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16437)
The Subtle Art of Not Giving a F*ck by Mark Manson(14341)
The 3rd Cycle of the Betrayed Series Collection: Extremely Controversial Historical Thrillers (Betrayed Series Boxed set) by McCray Carolyn(14124)
Stepbrother Stories 2 - 21 Taboo Story Collection (Brother Sister Stepbrother Stepsister Taboo Pseudo Incest Family Virgin Creampie Pregnant Forced Pregnancy Breeding) by Roxi Harding(13606)
Scorched Earth by Nick Kyme(12759)
Drei Generationen auf dem Jakobsweg by Stein Pia(10959)
Suna by Ziefle Pia(10883)
Scythe by Neal Shusterman(10329)
International Relations from the Global South; Worlds of Difference; First Edition by Arlene B. Tickner & Karen Smith(9516)
Successful Proposal Strategies for Small Businesses: Using Knowledge Management ot Win Govenment, Private Sector, and International Contracts 3rd Edition by Robert Frey(9359)
This is Going to Hurt by Adam Kay(9165)